﻿using System.Security.Cryptography;
using Aurora.Security.RSA.Config;
using Aurora.Security.RSA.Facility;

namespace Aurora.Security.RSA.Util
{
    public class RSAUtil : IRSAUtil
    {
        private RSAPkcs1Facility _pkcs1;

        private readonly RSAKey _rsaKey;

        public RSAUtil()
        {
            // _rsaKey = RSAKeyGenerator.Pkcs1Key(2048, false);
            //_rsaKey = RSAKeyGenerator.Pkcs1Key(2048, false);
            //_pkcs1 = new RSAPkcs1Facility(SecurityRSAContext.DefaultPublicKey,"" );
            _pkcs1 = new RSAPkcs1Facility("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwtGdKVMqrPlpLknC+vR1Iz6iTZLQSZTtuMYccQlYSBjl4Fn0U1jDdl1qzR4/3eGUvnXm2zXbOymqAtuFvmZUjBNTeSxFbgxsSz1KbME0YWQ8lX/mgTHALElfo+m3+ndSub4ziPSpeoxHln94bE6cnAU7ItnDsmkDNqNcBuVKfBkzAI9EVvQPHgNevvtzJUf8Vp4l9UNgPQ7aD2ldplU1RTEB/JQcMOpcBrfyDgi801aeZZywlf9nc0AFmbyxt68+tKZK1wL16Flc40pDm+5dQEyTL9h9nzPW6SEDDWw/tgecQCh58jM9xERJbt5EK1i0cv7gtq/3HuSQ0+0AsVSzGwIDAQAB",
                "MIIEpAIBAAKCAQEAwtGdKVMqrPlpLknC+vR1Iz6iTZLQSZTtuMYccQlYSBjl4Fn0U1jDdl1qzR4/3eGUvnXm2zXbOymqAtuFvmZUjBNTeSxFbgxsSz1KbME0YWQ8lX/mgTHALElfo+m3+ndSub4ziPSpeoxHln94bE6cnAU7ItnDsmkDNqNcBuVKfBkzAI9EVvQPHgNevvtzJUf8Vp4l9UNgPQ7aD2ldplU1RTEB/JQcMOpcBrfyDgi801aeZZywlf9nc0AFmbyxt68+tKZK1wL16Flc40pDm+5dQEyTL9h9nzPW6SEDDWw/tgecQCh58jM9xERJbt5EK1i0cv7gtq/3HuSQ0+0AsVSzGwIDAQABAoIBACQrFDli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sMJNLikN6gfUS4HRB5fXYil0ApaMfoEL8vdW0LTRw60iYN1xEHJvF6em3welPyvA3nPghf3w8yAkx5MbvjVMi/7AcmZA30ny5hFYvzPcfqeh1HWq9XkVyNgpFoy2fm7XlrpcbAFHfw==");
        }

        /// <summary>
        /// 生成keys
        /// </summary>
        /// <returns></returns>
        public async Task<RSAKey> GenerateKeysAsync()
        {
            return await Task.Run(() => _rsaKey);
        }

        /// <summary>
        /// 填充参数生成私钥或公钥
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public async Task SetRSAParametersAsync(RSAParameters parameters)
        {
            await Task.Run(() => { _pkcs1 = new RSAPkcs1Facility(parameters); });
        }

        /// <summary>
        /// 填充公钥
        /// </summary>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public async Task SetPublicKeyAsync(string publicKey)
        {
            await Task.Run(() => { _pkcs1 = new RSAPkcs1Facility(publicKey, ""); });
        }

        /// <summary>
        /// 填充私钥
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public async Task SetPrivateKeyAsync(string privateKey)
        {
            await Task.Run(() => { _pkcs1 = new RSAPkcs1Facility("", privateKey); });
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> EncryptAsync(string data)
        {
            return await _pkcs1.EncryptAsync(data, RSAEncryptionPadding.Pkcs1);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> DecryptAsync(string data)
        {
            return await _pkcs1.DecryptAsync(data, RSAEncryptionPadding.Pkcs1);
        }

        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> SignDataAsync(string data)
        {
            return await _pkcs1.SignDataAsync(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
        }

        /// <summary>
        /// 验签
        /// </summary>
        /// <param name="data">需要验签的数据</param>
        /// <param name="sign">已经签名的数据</param>
        /// <returns></returns>
        public async Task<bool> VerifyDataAsync(string data, string sign)
        {
            return await _pkcs1.VerifyDataAsync(data, sign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
        }
    }
}